package com.jy.api.controller.web;

import com.jy.api.dao.MemberDao;
import com.jy.api.dao.OrderDao;
import com.jy.api.domain.Member;
import com.jy.api.domain.Order;
import com.jy.api.exception.MyException;
import com.jy.api.service.MemberService;
import com.jy.api.service.OrderService;
import com.jy.api.util.ISyncExecutor;
import com.jy.api.util.RedisSyncLocker;
import com.jy.api.vo.web.MemberVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * Created by lihu on 2017/12/6.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/web", produces = "application/json;charset=utf-8")
public class WebMemberController {


    @Autowired
    OrderDao orderDao;


    @Autowired
    MemberService memberService;

    @Autowired
    MemberDao memberDao;

    @Autowired
    private OrderService orderService;
    @Autowired
    private RedisSyncLocker redisSyncLocker;

    /**
     * 获取会员信息
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/stores/{storeId}/member-info")
    public ResponseEntity queryMember(@PathVariable String storeId,
                                      @RequestParam(name = "memberId") String memberId) {
        try {
            MemberVo memberVo = memberService.getMemberInfo(storeId, memberId);
            return new ResponseEntity(memberVo, HttpStatus.OK);
        } catch (MyException ex) {
            log.error("getMemberInfo is error==> storeId:{},memberId:{}", storeId, memberId, ex);
            return new ResponseEntity(ex.getMessage(), HttpStatus.BAD_REQUEST);
        } catch (Exception ex) {
            log.error("getMemberInfo is error==> storeId:{},memberId:{}", storeId, memberId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 获取会员信息
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{orderId}/member-info")
    public ResponseEntity getMemberInfo(@PathVariable String orderId) {
        try {
            return (ResponseEntity) redisSyncLocker.idLock(orderId, o -> {
                Order order = orderDao.findOne(orderId);
                if (order == null) {
                    return new ResponseEntity("查询失败！订单信息不存在！", HttpStatus.BAD_REQUEST);
                }
                MemberVo memberVo = null;
                //订单绑定会员信息
                if (StringUtils.isNotBlank(order.getMemberId())) {
                    memberVo = memberService.getMemberInfo(order.getStoreId(), order.getMemberId());
                    memberVo.setOrder(orderService.judgeWebOrderItem(order));
                }

                return new ResponseEntity(memberVo, HttpStatus.OK);
            });
        } catch (MyException ex) {
            log.error("getMemberInfo is error==> orderId:{}", orderId, ex);
            return new ResponseEntity(ex.getMessage(), HttpStatus.BAD_REQUEST);
        } catch (Exception ex) {
            log.error("getMemberInfo is error==> orderId:{}", orderId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 获取会员信息
     *
     * @param orderId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/member")
    public ResponseEntity getMember(@RequestParam(name = "orderId") String orderId,
                                    @RequestParam(name = "openId") String openId) {
        try {
            Order order = orderDao.findOne(orderId);
            if (order == null) {
                return new ResponseEntity("查询失败！订单信息不存在！", HttpStatus.BAD_REQUEST);
            }
            Member member = memberDao.findOne(order.getMemberId());
            if (member == null || !StringUtils.equals(member.getOpenId(), openId)) {
                return new ResponseEntity("请使用安全登陆微信号进行扫码支付！", HttpStatus.BAD_REQUEST);
            }
            MemberVo memberVo = null;
            //订单绑定会员信息
            if (StringUtils.isNotBlank(order.getMemberId())) {
                memberVo = memberService.getMemberInfo(order.getStoreId(), member);
            }
            return new ResponseEntity(memberVo, HttpStatus.OK);
        } catch (MyException ex) {
            log.error("getMemberInfo is error==> orderId:{}", orderId, ex);
            return new ResponseEntity(ex.getMessage(), HttpStatus.BAD_REQUEST);
        } catch (Exception ex) {
            log.error("getMemberInfo is error==> orderId:{}", orderId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
